import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_page/community_business/post_detail_page/community_image_page.dart';
import 'package:tiktokadult/tools_base/widget/card_swiper/src/swiper.dart';
import 'package:tiktokadult/tools_base/widget/net_image_widget.dart';

import '../../hj_model/splash/ads_model.dart';
import '../../hj_utils/screen.dart';

/// 图片广告
class AdsImageWidget extends StatelessWidget {
  final AdsInfoModel? model;
  final double? width;
  final double? height;
  final VoidCallback? onTap;

  const AdsImageWidget(this.model, {Key? key, this.width, this.height, this.onTap}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: SizedBox(
        width: width,
        height: height,
        child: NetworkImageLoader(
          fit: BoxFit.fill,
          // placeholder: Container(
          //   color: const Color(0x00000000),
          // ),
          imageUrl: model?.cover ?? '',
        ),
      ),
    );
  }
}

/// 广告轮播
class AdsBannerWidget extends StatefulWidget {
  final List<AdsInfoModel>? models;
  final double? width;
  final double? height;
  final ValueChanged<int>? onItemClick;
  final ValueChanged<int>? onIndexChanged;
  final int? autoPlayDuration;
  final double? radius;
  final bool isDarkWebStyle;
  final bool isIndicatorBottomCenter;
  final Widget? placeHolderWidget;

  const AdsBannerWidget(
    this.models, {
    Key? key,
    this.width,
    this.height,
    this.autoPlayDuration = 5000,
    this.onItemClick,
    this.onIndexChanged,
    this.radius,
    this.isDarkWebStyle = false,
    this.isIndicatorBottomCenter = false,
    this.placeHolderWidget,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _AdsBannerWidgetState();
  }
}

class _AdsBannerWidgetState extends State<AdsBannerWidget> {
  int selectIndex = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    double? moduleW = widget.width;
    double? moduleH = widget.height;
    return SizedBox(
      width: moduleW,
      height: moduleH,
      child: Stack(
        clipBehavior: Clip.none,
        children: <Widget>[
          Swiper(
            index: selectIndex,
            autoplay: ((widget.models?.length ?? 0) > 1),
            autoplayDelay: widget.autoPlayDuration ?? 3,
            loop: widget.models?.length == 1 ? false : true,
            itemBuilder: (BuildContext context, int index) {
              var cover = widget.models![index].cover;
              return SizedBox(
                width: screen.screenWidth,
                child: NetworkImageLoader(
                  fit: BoxFit.fill,
                  imageUrl: cover ?? '',
                  borderRadius: widget.radius ?? 0,
                  placeHolderWidget: widget.placeHolderWidget,
                ),
              );
            },
            onTap: widget.onItemClick,
            onIndexChanged: (int index) {
              setState(() {
                selectIndex = index;
              });
              if (widget.onIndexChanged != null) {
                widget.onIndexChanged!(index);
              }
            },
            itemCount: widget.models?.length ?? 0,
          ),
          if (widget.isIndicatorBottomCenter)
            Positioned(
              bottom: -12,
              right: 0,
              left: 0,
              child: (widget.models?.length ?? 0) > 1
                  ? Container(
                      alignment: Alignment.center,
                      child: CIndicator(
                        itemCount: widget.models?.length ?? 0,
                        selectIndex: selectIndex,
                        space: 6,
                      ),
                    )
                  : Container(),
            )
          else
            Positioned(
              bottom: 7,
              right: 7,
              child: (widget.models?.length ?? 0) > 1
                  ? CIndicator(
                      itemCount: widget.models?.length ?? 0,
                      selectIndex: selectIndex,
                      space: 6,
                    )
                  : Container(),
            ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}

/// 分页指示器
class CIndicator extends StatefulWidget {
  const CIndicator({
    Key? key,
    this.itemCount,
    this.selectIndex,
    this.space = 5.0,
    this.callBack,
    this.isDarkWebStyle = false,
  }) : super(key: key);

  ///
  final int? itemCount;
  final int? selectIndex;
  final double? space;
  final Function(int)? callBack;
  final bool isDarkWebStyle;

  @override
  State<StatefulWidget> createState() {
    return _CIndicatorState();
  }
}

class _CIndicatorState extends State<CIndicator> {
  @override
  void initState() {
    super.initState();
  }

  Widget _getBall(int index) {
    var normalBall = Padding(
      padding: EdgeInsets.only(left: widget.space ?? 0),
      child: ClipOval(
        child: Container(
          width: 6,
          height: 6,
          color: Colors.white.withOpacity(0.5),
        ),
      ),
    );
    normalBall = Padding(
      padding: EdgeInsets.only(left: widget.space ?? 0),
      child: ClipOval(
        child: Container(
          width: 6,
          height: 6,
          color: Colors.white,
        ),
      ),
    );
    var selectBall = Padding(
        padding: EdgeInsets.only(left: widget.space ?? 0),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(5),
          child: Container(
            width: 12,
            height: 6,
            color: Colors.white,
          ),
        ));
    selectBall = Padding(
        padding: EdgeInsets.only(left: widget.space ?? 0),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(10),
          child: Container(
            width: 18,
            height: 6,
            color: widget.isDarkWebStyle ? Color(0xffE1351F) : Color(0xffE1351F),
          ),
        ));
    return (widget.selectIndex == index) ? selectBall : normalBall;
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> list = [];
    for (int i = 0; i < (widget.itemCount ?? 0); ++i) {
      list.add(_getBall(i));
    }
    var wd = Row(
      mainAxisSize: MainAxisSize.min,
      children: list,
    );
    return wd;
  }
}

//图集数据,左右滑动组件
class ImageCollectionWidget extends StatefulWidget {
  final List<String>? imgs;
  final double aspectRatio;
  final bool showCIndicator; //是否显示指示器
  final bool showIndex;

  ImageCollectionWidget({
    super.key,
    this.imgs,
    this.aspectRatio = 375 / 467,
    this.showCIndicator = true,
    this.showIndex = true,
  });

  @override
  State<ImageCollectionWidget> createState() => _ImageCollectionStateWidget();
}

class _ImageCollectionStateWidget extends State<ImageCollectionWidget> {
  int selectIndex = 0;

  @override
  Widget build(BuildContext context) {
    return AspectRatio(
        aspectRatio: widget.aspectRatio,
        child: Stack(
          alignment: Alignment.center,
          children: [
            Swiper(
              autoplay: ((widget.imgs?.length ?? 0) > 1),
              loop: widget.imgs?.length == 1 ? false : true,
              itemBuilder: (BuildContext context, int index) {
                var cover = widget.imgs![index];
                return InkWell(
                  onTap: () {
                    Get.to(CommunityImagePage(
                      imageList: widget.imgs,
                      index: index,
                    ));
                  },
                  child: NetworkImageLoader(imageUrl: cover),
                );
              },
              onIndexChanged: (int index) {
                setState(() {
                  selectIndex = index;
                });
              },
              itemCount: widget.imgs?.length ?? 0,
            ),
            if (widget.showIndex && (widget.imgs?.length ?? 0) > 1)
              Positioned(
                  top: 24,
                  right: 16,
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 14),
                    alignment: Alignment.center,
                    height: 28,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(999),
                      color: Colors.black.withOpacity(.6),
                    ),
                    child: Text(
                      '${selectIndex + 1}/${widget.imgs?.length ?? 0}',
                      style: TextStyle(fontSize: 14, color: Colors.white),
                    ),
                  )),
            if (widget.showCIndicator && (widget.imgs?.length ?? 0) > 1)
              Positioned(
                bottom: 10,
                child: CIndicator(
                  itemCount: widget.imgs?.length ?? 0,
                  selectIndex: selectIndex,
                  space: 6,
                ),
              ),
          ],
        ));
  }
}
